<template>
  <div class="goods-list-item" @click="goDetail(goodsListItem.iid)">
    <img v-lazy="showImg" @load="imgLoad" />
    <div class="goods-info">
      <p>{{ goodsListItem.title }}</p>
      <span class="price">{{ goodsListItem.price }}</span>
      <span class="cfav">{{ goodsListItem.cfav }}</span>
    </div>
  </div>
</template>

<script>
export default {
  name: "GoodsListItem",
  props: {
    goodsListItem: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  computed: {
    // 当组件复用，一些属性不同时，使用计算属性匹配对应的属性
    showImg() {
      return (
        this.goodsListItem.img ||
        this.goodsListItem.image ||
        this.goodsListItem.show.img
      );
    }
  },
  methods: {
    // 向外发射imgLoad事件，通知图片已被加载完成
    // 潜在的问题： goodsListItem 组件被连个home组件和detail组件共用，此暴露的事件goodsItemImgLoad会在访问其中一个组件时访问另一个组件，需对暴露的事件进行取消
    imgLoad() {
      this.$bus.$emit("goodsItemImgLoad");
    },
    goDetail(iid) {
      this.$router.push({
        path: "/detail/" + iid
      });
    }
  }
};
</script>

<style lang="less" scoped>
.goods-list-item {
  position: relative;
  padding-bottom: 40px;
  width: 48%;
  img {
    width: 100%;
  }
  .goods-info {
    text-align: center;
    font-size: 12px;
    overflow: hidden;

    font-size: 12px;
    position: absolute;
    bottom: 5px;
    left: 0;
    right: 0;
    overflow: hidden;
    text-align: center;
    p {
      padding: 3px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    span {
      display: inline-block;
      width: 50%;
    }
    .price {
      color: var(--color-tint);
    }
    .cfav {
      position: relative;
      &::before {
        content: "";
        position: absolute;
        left: 25px;
        top: 0px;
        width: 14px;
        height: 14px;
        background: url("~assets/img/common/collect.svg") 0 0 / 14px 14px;
      }
    }
  }
}
</style>
